// product/pages/sceneTheatre/sceneTheatre.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sceneList:[],
    page: {
      currentPage: 1,
      pageSize: 10,
      total: 0,
    },
    isGetSceneList:false,
    isEmpty:false,
    isLoadMore:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSceneList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
    //滚动到底部触发
    scrolltolower() {
      this.loadMore();
    },
    loadMore: function () {
      const { currentPage, total } = this.data.page;
      if (
        total > this.data.sceneList.length &&
        this.data.isGetSceneList === false
      ) {
        this.setData({
          "page.currentPage": currentPage + 1,
          isLoadMore: true,
        });
        this.getSceneList();
      }
    },
    async getSceneList() {
      console.log("this.data.isGetSceneList", this.data.isGetSceneList);
      if (this.data.isGetSceneList) return;
  
      let { currentRepo, page, sceneList } = this.data;
      const { currentPage, pageSize } = page;
      try {
        this.data.isGetSceneList = true;
        let res = await app.api.get({
          url: "theater_vx_query",
          data: { currentPage,pageSize },
        });
        console.log('getSceneList',res)
        let isEmpty = false;
        let arr = res;
        arr.forEach((item,index)=>{
          let time = new Date(item.videoModified)
          arr[index].updateTime = (time.getMonth() + 1) + '.' + time.getDate()
        })
        let list = this.data.sceneList
        console.log('this.data',this.data)
        if (this.data.isLoadMore) {
          console.log("执行加载");
          list.push(...arr);
        } else {
          console.log("执行加载2");
          list = arr;
        }
        if (list.length <= 0) {
          isEmpty = true;
        }
        console.log('list',list)
        this.setData({
          sceneList: list,
          isLoadMore: false,
          isEmpty: isEmpty,
        });
        console.log('getSceneList',this.data)
  
        this.data.isGetSceneList = false;
      } catch (e) {
        console.log('error',e)
        this.data.isGetSceneList = false;
      }
    },
  toDetail(e){
    console.log(e)
    let item = e.currentTarget.dataset.item;
    console.log(item)
    wx.navigateTo({ url: `/product/pages/sceneShow/sceneShow?id=${item.id}`,
    success: function(res) {
      // 通过eventChannel向被打开页面传送数据
      res.eventChannel.emit('sceneDetailDataEvent', { detailInfo: item })

    }
  });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})